﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FishSimulation.Model;

namespace FishSimulation.Views
{
    /// <summary>
    /// Interaction logic for FoodValueFormulaView.xaml
    /// </summary>
    public partial class FormulaEditorView : ViewBase {
        #region Properties
        public bool Canceled {
            get;
            set;
        }

        public string Formula {
            get {
                return txtFormula.Text;
            }
            set {
                txtFormula.Text = value;
            }
        }

        public bool ShowOtherPropreties {
            get;
            set;
        }
        #endregion

        #region Constructors
        public FormulaEditorView()
        {
            InitializeComponent();
            this.Canceled = true;
            this.ShowOtherPropreties = true;
            
            this.Loaded += delegate {
                if (this.ShowOtherPropreties) {
                    lbWorld.ItemsSource = WorldProperties.Instance.PropertyNames;
                    lbActorType.ItemsSource = ActorTypeProperties.Instance.PropertyNames;
                    lbActor.ItemsSource = ActorProperties.Instance.PropertyNames;
                }
                else {
                    lbWorld.ItemsSource = WorldProperties.Instance.PropertyNames.Where(x => !x.StartsWith("Other"));
                    lbActorType.ItemsSource = ActorTypeProperties.Instance.PropertyNames.Where(x => !x.StartsWith("Other"));
                    lbActor.ItemsSource = ActorProperties.Instance.PropertyNames.Where(x => !x.StartsWith("Other"));
                }

                if (txtFormula.Text == "") {
                    txtFormula.Text += @"#the value of this textbox can be a constant like 1, 10, etc.. OR";
                    txtFormula.Text += @"#a Python script that sets the value of a variable called 'result'{0}".Formatt(Environment.NewLine);
                    txtFormula.Text += @"#example: result = 1{0}".Formatt(Environment.NewLine);
                }
            };
        }
        #endregion

        #region Event Handlers
        private void btnOk_Click(object sender, System.Windows.RoutedEventArgs e) {
            this.Canceled = false;
            var win = Window.GetWindow(this);
            if (win != null) {
                win.Close();
            }
        }

        public void ListboxItem_DoubleClick(object sender, MouseButtonEventArgs e) {
            ListBoxItem item = sender as ListBoxItem;
            if (item != null) {
                txtFormula.Text = txtFormula.Text.Insert(txtFormula.CaretIndex, "{{{0}}}".Formatt(item.Content.ToString()));
            }
        }
        #endregion
    }
}
